package com.xh.sso;

import com.xh.model.AdDepartment;
import com.xh.model.User;
import com.xh.util.ADProperty;
import com.xh.util.ADUtils;

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.LdapContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Hashtable;
import java.util.List;
import java.util.TreeSet;

public class getADInfo {

    private static final Log log = LogFactory.getLog(getADInfo.class);
    public static Hashtable<String, String> env = new Hashtable<String, String>();

    static {
        String adminName = ADProperty.get("Ldap.adminName");// username
        String adminPassword = ADProperty.get("Ldap.adminPassword");// password
        String ldapURL = ADProperty.get("Ldap.ldapURL");// ip:port

        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.SECURITY_AUTHENTICATION, ADProperty.get("Ldap.security_authentication"));// LDAP访问安全级别："none","simple","strong"
        env.put(Context.SECURITY_PRINCIPAL, adminName);// AD User
        env.put(Context.SECURITY_CREDENTIALS, adminPassword);// AD Password
        env.put(Context.PROVIDER_URL, ldapURL);// LDAP工厂类
        env.put("com.sun.jndi.ldap.connect.timeout", ADProperty.get("Ldap.connect.timeout"));// 连接超时设置为3秒
    }

    // 获取ad用户
    public static JSONArray getAdUsersArray() {
        LdapContext ctx = null;
        JSONArray jsonArray = null;
        try {
            ctx = ADUtils.getContext(env);
            List<User> users = ADUtils.getUsers(ctx, ADProperty.get("Ldap.searchBase"));
            jsonArray = JSONArray.fromObject(users);
            log.debug("AD域人员List:" + jsonArray.toString());

        } catch (Exception e) {
            log.error("同步AD域用户信息异常!", e);

        } finally {
            if (ctx != null) {
                try {
                    ctx.close();
                    ctx = null;
                } catch (Exception e) {
                    log.warn("关闭AD域用户信息-连接失败!");
                }
            }
        }

        return jsonArray;
    }

    // 获取ad部门树结构
    public static JSONObject getADDepartmentObject() {
        LdapContext ctx = null;
        JSONObject jsonObject = null;
        String searchBase = ADProperty.get("Ldap.searchBase");
        try {
            ctx = ADUtils.getContext(env);
            TreeSet<AdDepartment> as = ADUtils.getAdDepartment(ctx, searchBase);

            String searchADBase = ADProperty.get("Ldap.searchADBase");
            if (searchADBase == null || searchADBase.length() <= 0) {
                searchADBase = searchBase.split(",")[searchBase.split(",").length - 2].split("=")[1] + "." + searchBase.split(",")[searchBase.split(",").length - 1].split("=")[1];
            }

            jsonObject = JSONObject.fromObject(ADUtils.getTreeAdDepartment(as, searchADBase));
            log.debug("AD域部门Tree:" + jsonObject.toString());

        } catch (NamingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            log.error("同步AD域组织信息异常!", e);

        } finally {
            if (ctx != null) {
                try {
                    ctx.close();
                    ctx = null;
                } catch (Exception e) {
                    log.warn("关闭AD域组织信息-连接失败!");
                }
            }
        }

        return jsonObject;
    }

    public static void main(String args[]) {
        getADDepartmentObject();
        getAdUsersArray();
    }
}
